//
//  LXNoticeScrollView.m
//  ZuLinProject
//
//  Created by bancheng on 2022/7/5.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXNoticeScrollView.h"
#import "CaoZuoTaiModel.h"
@interface LXNoticeScrollView()
/// timer
@property (nonatomic, strong) NSTimer *scrollTimer;

@property (nonatomic, strong) UILabel *tipsLabel;

@end

@implementation LXNoticeScrollView

- (instancetype)initWithFrame:(CGRect)frame{
  self = [super initWithFrame:frame];
  if (self) {
    [self addSubview:self.bgScrollView];
    
  }
  return self;
}
- (void)setTitleArr:(NSArray *)titleArr{
    _titleArr = titleArr;
    
    if (self.titleArr.count != 0) {
        
        // 先把所有视图和定时器停止
        [self.bgScrollView removeAllSubviews];
        [self closeTimer];
        
        self.tipsLabel.hidden = YES;
        [self createBaseView];
        [self openTimer];
    }
    else {
        self.tipsLabel.hidden = NO;
    }
    
}
// MARK: - 开启定时器
- (void)openTimer {
  if (!_scrollTimer) {
    _scrollTimer = [NSTimer scheduledTimerWithTimeInterval:7 target:self selector:@selector(timerMoved) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_scrollTimer forMode:NSRunLoopCommonModes];
  }
}
 
// MARK: - 关闭定时器
- (void)closeTimer {
  [_scrollTimer invalidate];
  _scrollTimer = nil;
}
 
- (UIScrollView *)bgScrollView {
  if (!_bgScrollView) {
    _bgScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
//    _bgScrollView.scrollEnabled = NO;
      _bgScrollView.pagingEnabled = YES;
    _bgScrollView.showsVerticalScrollIndicator = NO;
    _bgScrollView.showsHorizontalScrollIndicator = NO;
    _bgScrollView.backgroundColor = UIColor.whiteColor;
  }
  return _bgScrollView;
}
 
// MARK: - 创建所有视图
- (void)createBaseView {
  // 安全判断
  if (self.titleArr.count == 0) {
    return;
  }
  // 为了展示滑动过程的流畅性，重新处理数组
  NSMutableArray *dataMArray = [NSMutableArray arrayWithCapacity:0];
  [dataMArray addObjectsFromArray:_titleArr];
  [dataMArray addObject:_titleArr.firstObject];
  for (int i = 0; i<dataMArray.count; i++) {
      
      
      CaoZuoTaiModel *model = dataMArray[i];
      UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height*(i%dataMArray.count), self.width, self.height)];
      backView.backgroundColor = [UIColor colorWithHexString:@"#FFFCF9"];
      [_bgScrollView addSubview:backView];
      UIImage *image = [UIImage imageNamed:@"日程提醒icon"];
      UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(15, 17, image.size.width, image.size.height)];
      imgV.image = image;
      [backView addSubview:imgV];
      
      
      UIView *line = [[UIView alloc] initWithFrame:CGRectMake(imgV.X_width + 12, 17, 1, 34)];
      line.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
      [backView addSubview:line];

      
      UIButton *timeBtn = [[UIButton alloc] initWithFrame:CGRectMake(line.X_width + 11, 17, self.width - line.X_width - 100, 18)];
      timeBtn.userInteractionEnabled = NO;
      timeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
      [timeBtn setImage:Image(@"home_时间_time") forState:UIControlStateNormal];
      [timeBtn setTitle:[NSString stringWithFormat:@" %@ %@",model.appointTimeFormat,model.typeDesc] forState:UIControlStateNormal];
      timeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
      [timeBtn setTitleColor:[UIColor colorWithHexString:@"#FC7F27"] forState:UIControlStateNormal];
      [backView addSubview:timeBtn];
      
      UIButton *addressBtn = [[UIButton alloc] initWithFrame:CGRectMake(timeBtn.X, timeBtn.Y_height + 3, timeBtn.width, 18)];
      addressBtn.userInteractionEnabled = NO;
      addressBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
      [addressBtn setImage:Image(@"home_已定位_local") forState:UIControlStateNormal];
      [addressBtn setTitle:model.address forState:UIControlStateNormal];
      addressBtn.titleLabel.font = [UIFont systemFontOfSize:12];
      [addressBtn setTitleColor:[UIColor colorWithHexString:@"#FC7F27"] forState:UIControlStateNormal];
      [backView addSubview:addressBtn];
      
  }
  _bgScrollView.contentSize = CGSizeMake(0, self.height*dataMArray.count);
    
    
    UIImage *gengduo = [UIImage imageNamed:@"gengduo"];
    UIButton *moreBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width - gengduo.size.width - 12, self.height - gengduo.size.height - 12, gengduo.size.width, gengduo.size.height)];
    [moreBtn setImage:gengduo forState:UIControlStateNormal];
    self.moreBtn = moreBtn;
    [self addSubview:moreBtn];
    
}
 
// MARK: - 定时器调用方法
- (void)timerMoved {
  CGFloat pageY = self.bgScrollView.contentOffset.y/self.height;
  int pageIntY = pageY;
  if (pageIntY >= self.titleArr.count) {
    [self.bgScrollView setContentOffset:CGPointMake(0, 0) animated:NO];
  } else {
    [self.bgScrollView setContentOffset:CGPointMake(0, (pageIntY+1)*self.height) animated:YES];
  }
}

- (UILabel *)tipsLabel{
    if (!_tipsLabel) {
        _tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 100, self.height)];
        _tipsLabel.text = @"  今日暂无日程";
        _tipsLabel.font = [UIFont systemFontOfSize:15];
        _tipsLabel.textColor = ColorWithAAAAAA;
        [_bgScrollView addSubview:_tipsLabel];
    }
    return _tipsLabel;
}

@end
